package com.itheima.tlias.controller;

import com.itheima.tlias.pojo.Clazz;
import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
	@Autowired
	private ClazzService clazzService;

	@GetMapping("/list")
	public Result list() {
		List<Clazz> list = clazzService.list();
		return Result.success(list);
	}

	@PostMapping
	public Result save(@RequestBody Clazz clazz) {
		clazzService.save(clazz);
		return Result.success();
	}

	@DeleteMapping("/{id}")
	public Result deleteById(@PathVariable("id") Integer id) {
		clazzService.deleteById(id);
		return Result.success();
	}

	@GetMapping("/{id}")
	public Result getById(@PathVariable("id") Integer id) {
		Clazz clazz = clazzService.getById(id);
		return Result.success(clazz);
	}

	@PutMapping
	public Result update(@RequestBody Clazz clazz) {
		clazzService.updateById(clazz);
		return Result.success();
	}

	@GetMapping
	public Result page(
			String name,
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
			@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize
	) {
		return Result.success(clazzService.page(name, begin, end, page, pageSize));
	}

	//@DeleteMapping("/{id}")
	//public Result deleteById(@PathVariable Integer id) {
	//	clazzService.deleteById(id);
	//	return Result.success();
	//}
}
